package Servlets.lhl;/**
 * 项目名称：ProEditServlet.java
 * 文件名称：ProEditServlet.java
 * 类的描述：无
 * 创建时间：2025-07-26 09:42:13
 * 创建人：大帅比
 * <p>
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */
 /*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/

import java.io.*;
import java.math.BigDecimal;

import Util.GokJDBCUtil2;
import Util.RES;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@WebServlet(name = "ProEditServlet", value = "/proedit")
public class ProEditServlet extends HttpServlet {

    public void init() {
    }


    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        String proname = req.getParameter("proname");
        String price = req.getParameter("price");
        String originplace = req.getParameter("originplace");
        String levelname = req.getParameter("levelname");
        String categoryname = req.getParameter("categoryname");
        String status = req.getParameter("status");
        String id = req.getParameter("id");
        String description = req.getParameter("description");
        //如果都不为空且有长度
        if( description!=null && !description.isEmpty() && proname != null && price != null && originplace != null && levelname != null && categoryname != null && status != null && id != null && proname.length() > 0 && price.length() > 0 && originplace.length() > 0 && levelname.length() > 0 && categoryname.length() > 0 && status.length() > 0 && id.length() > 0){
            Integer id1 = Integer.parseInt(id);
            //levelname和categorynameh转成int
            int i = Integer.parseInt(levelname);
            int i1 = Integer.parseInt(categoryname);
            int i2 = Integer.parseInt(status);
            //price 转换为BigDecimal
            BigDecimal price1 = new BigDecimal(price);
            //i2是否在0和1之间
            if(i2 != 0 && i2 != 1){
                res.getWriter().write(RES.fail("参数错误"));
                return;
            }
            String sql="update product set proname=?,price=?,originplace=?,levelid=?,categoryid=?,status=?,description=? where id=?";
            GokJDBCUtil2.update(sql, proname, price1, originplace, i, i1, i2,description, id1);
            res.getWriter().write(RES.success("修改成功"));
        }

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String method = req.getMethod();
        try {
            if (method.equals("POST")) {
                this.doPost(req, res);
            } else {
                PrintWriter writer = res.getWriter();
                writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
                writer.close();
            }
        } catch (Exception e) {
            log.error("服务器处理请求时发生错误", e);
            PrintWriter writer = res.getWriter();
            writer.println("{\"code\":\"500\",\"msg\":\"服务器处理请求时发生错误\"}");
            writer.close();
        }
    }

    public void destroy() {
    }
}